package com.atguigu.tingshu.album.task;

import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sound.midi.Track;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2024-11-25 16:05
 */
@Slf4j
@Component
public class ReviewResultTask {

    @Autowired
    private TrackInfoMapper trackInfoMapper;

    @Autowired
    private VodService vodService;

    /**
     * 开启定时任务，每5秒执行一次，查询处于审核中声音记录，根据声音中审核任务ID查询审核结果
     * 根据审核建议更新声音状态
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void reviewResultTask() {
        log.info("开启审核任务");
        //1.根据审核状态为：0503（审核中）声音列表
        LambdaQueryWrapper<TrackInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackInfo::getStatus, SystemConstant.TRACK_STATUS_REVIEWING);
        queryWrapper.select(TrackInfo::getId, TrackInfo::getReviewTaskId);
        queryWrapper.last("limit 100");
        List<TrackInfo> trackInfoList = trackInfoMapper.selectList(queryWrapper);

        //2.遍历声音列表，根据声音中审核任务ID查询审核结果
        if (CollectionUtil.isNotEmpty(trackInfoList)) {
            //3.根据建议结果更新声音状态
            for (TrackInfo trackInfo : trackInfoList) {
                String suggest = vodService.getReviewTaskSuggest(trackInfo.getReviewTaskId());
                if ("pass".equals(suggest)) {
                    trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
                } else if ("block".equals(suggest)) {
                    trackInfo.setStatus(SystemConstant.TRACK_STATUS_NO_PASS);
                } else if ("review".equals(suggest)) {
                    trackInfo.setStatus(SystemConstant.TRACK_STATUS_ARTIFICIAL);
                }
                trackInfoMapper.updateById(trackInfo);
            }
        }
    }
}
